package com.wangmei.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangmei.model.Address;
import com.wangmei.service.AddressService;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import unitTool.Dictionary;
import unitTool.JsonRsp;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/addre")
public class AddressController {
    @Autowired
    private AddressService addressService;
    /**
     * 获得全部地址
     * @return
     */
    @ApiOperation(value = "获取路径")
    @RequestMapping(value = "/getAddres",method = RequestMethod.POST)
    public JsonRsp getAddres(
            @RequestParam(value="uid",required = false) Integer uid
            ,@RequestParam(value = "pageIndex",defaultValue = Dictionary.PageDirect.PAGE_INDEX)
                                         int pageIndex
            ,@RequestParam(value = "pageSize", defaultValue = Dictionary.PageDirect.PAGE_SIZE)
                                         int pageSize){
//        try{}catch (Exception e){}
        try{ PageHelper.startPage(pageIndex,pageSize);
            Map<String,Object> param = new HashMap<>();
            if(uid != null)
                param.put("uid",uid);
            PageInfo pageInfo = new PageInfo(addressService.queryAll(param));
            return JsonRsp.success(pageInfo.getList()).setTotal(pageInfo.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return JsonRsp.fail(4000,"系统内部错误");
        }

    }

    /**
     * 添加地址
     * @return
     */
    @ApiOperation(value = "添加路径")
    @RequestMapping(value = "/addAddres",method = RequestMethod.POST)
    public JsonRsp addAddres(@RequestBody Address address){
        try{
            return JsonRsp.success(addressService.inserAddr(address),"success");
        }catch (Exception e){
            e.printStackTrace();
            return JsonRsp.fail(4000,"系统内部错误");
        }

    }

    /**
     * 删除地址
     * @return
     */
    @ApiOperation(value = "删除路径")
    @RequestMapping(value = "/deleteAddres",method = RequestMethod.POST)
    public JsonRsp deleteAddres(@RequestParam(value = "addressid", defaultValue = "0") int addressid){
        try{
            return JsonRsp.success(addressService.deleteAddr(addressid),"success");
        }catch (Exception e){
            e.printStackTrace();
            return JsonRsp.fail(4000,"系统内部错误");
        }
    }
}
